package com.lzh.yupao.once;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.lzh.yupao.mapper.UserMapper;
import com.lzh.yupao.model.domain.User;
import com.lzh.yupao.service.UserService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import javax.annotation.Resource;

/**
 * @author LZH
 * @date 2023/5/16
 */
//@Component
public class InsertUsers {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserService userService;

    /**
     * 批量插入用户
     */
    @Scheduled(initialDelay = 5000, fixedRate = Long.MAX_VALUE)
    public void doInsertUsers() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        final int insertNum = 10000000;

        List<User> userList = new ArrayList<>();
        for (int i = 9999; i < insertNum; i++) {
            User user = new User();
            user.setUsername("假用户_" + i);
            user.setUserAccount("假账号_" + i);
            user.setAvatarUrl("https://img2.baidu.com/it/u=2080244369,3435160753&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500");
            user.setGender(0);
            user.setUserPassword("123456");
            user.setPhone("12334566789");
            user.setEmail("aaaa@qq.com");
            user.setUserStatus(0);
            user.setUserRole(0);
            user.setPlanetCode((System.currentTimeMillis() + "").substring(0, 6));
            user.setTags("[]");
            userList.add(user);
        }
        userService.saveBatch(userList, 100);

        stopWatch.stop();
        System.out.println(stopWatch.getTotalTimeMillis());
    }
}
